# 多进程实现股票爬虫
import requests
import re
import time
from multiprocessing import Pool

# 股票的的代码列表
stock = ['300657', '300658', '300659', '300660', '300661', '300662', '300663', '300664', '300665', '300666', '300667',
         '300668', '300669', '300670']

m1 = re.compile(r"price: '(.*\..*)'")


# 获取股票的价格信息
def GetPrice(code):
    try:
        url = 'http://quotes.money.163.com/1%s.html' % code
        txt = requests.get(url).text
        price = m1.findall(txt)[0]
        print(code, price)
    except:
        pass


if __name__ == "__main__":
    start = time.time()
    # 创建进程池
    p = Pool()
    for code in stock:
        p.apply_async(GetPrice, args=(code,))
    p.close()
    p.join()
    print("耗时：", time.time() - start)